<?php
/*
Plugin Name: eSfaturi.ro - Category related posts widget
Plugin URI: 
Description: Displays category related posts
Version: 1.0.0
Author: Sergiu Bologa
Author URI: 
License: GPL2
*/

// Start class
class esf_category_related_posts_widget extends WP_Widget {
	
	// Constructor
	function esf_category_related_posts_widget() {
		$widget_ops = array( 'classname' => 'esf_category_related_posts_widget', 'description' => 'Displays category related posts' );
		
		// Widget Settings
		$control_ops = array( 'id_base' => 'esf_category_related_posts_widget' ); // Widget Control Settings
		$this->WP_Widget( 'esf_category_related_posts_widget', 'Category related posts widget', $widget_ops, $control_ops ); // Create the widget
	}

	// Extract Args
	function widget($args, $instance) {
		extract( $args );
		$title = apply_filters('widget_title', $instance['title']); // the widget title
		$numberOfPosts = $instance['numberOfPosts'];

		//display widget only for posts
		if ( is_single() ) {
			// Before widget
			echo $before_widget;
			
			// Title of widget
			if ( $title ) { echo $before_title . $title . $after_title; }

			// Widget output
			global $post;
			$tempPost = $post;
			$category = get_the_category(); 
			$catId = $category[0]->parent != 0 ? $category[0]->parent : $category[1]->parent;
			$args = array('numberposts' => $numberOfPosts, 'exclude' => $post->ID, 'category' => $catId, 'orderby' => 'rand');
			//print_r($args);
			$myposts = get_posts( $args );
			//if the min number of posts is not reached, add other random posts to the related posts list
			if (count($myposts) < $numberOfPosts)
			{
				$excludeIds[0] = $post->ID;
				foreach( $myposts as $mypost )
					array_push($excludeIds, $mypost->ID);
			
				$remainingPostsNumber = $numberOfPosts - count($myposts);
				$args = array( 'numberposts' => $remainingPostsNumber, 'exclude' => $excludeIds, 'orderby' => 'rand' );
				$otherPosts = get_posts( $args );
				foreach( $otherPosts as $otherPost )
					array_push($myposts, $otherPost);
			}
			//print_r($myposts);
			foreach( $myposts as $post ) :	setup_postdata($post);
				$url = get_permalink();
				$title = get_the_title();
				$excerpt = get_the_excerpt();
				$excerpt = string_limit_words($excerpt, 10);
				$img = get_the_post_thumbnail($post->ID, 'thumb100x100', array('class' => 'alignleft'));
				echo "<div class='relatedPost'>
						<a href='$url' title='$title'>$img</a>
						<a class='relatedPostTitle' href='$url' title='$title'>$title</a>
						<p>$excerpt...</p>
					  </div>";
			endforeach;
			//echo $post->ID;
			$post = $tempPost;

			// After widget
			echo $after_widget;
		}
	}
	
	// Update Settings
	function update($new_instance, $old_instance) {
 		$instance['title'] = strip_tags($new_instance['title']);
		$instance['numberOfPosts'] = $new_instance['numberOfPosts'];
 		return $instance;
 	}
	
	// Widget Control Panel
	function form($instance) {
 		$defaults = array( 'title' => 'Category Related Posts', 'numberOfPosts' => '3' );
 		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

 		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
 			<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance['title']; ?>" />
 		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('numberOfPosts'); ?>">Number of posts:</label>
 			<input class="widefat" id="<?php echo $this->get_field_id('numberOfPosts'); ?>" name="<?php echo $this->get_field_name('numberOfPosts'); ?>" type="text" value="<?php echo $instance['numberOfPosts']; ?>" />
 		</p>
 			
        <?php 
	}
}

// End class category_related_posts_widget
add_action('widgets_init', create_function('', 'return register_widget("esf_category_related_posts_widget");'));
?>